//
//  CDSideBarController.m
//  CDSideBar
//
//  Created by Christophe Dellac on 9/11/14.
//  Copyright (c) 2014 Christophe Dellac. All rights reserved.
//

#import "CDSideBarController.h"

@implementation CDSideBarController

@synthesize menuColor = _menuColor;
@synthesize isOpen = _isOpen;

#pragma mark - 
#pragma mark Init

- (CDSideBarController*)initWithTitles:(NSArray*)titles
{
    _menuButtonBackgroundView = [[UIView alloc] init];
    _menuButtonBackgroundView.backgroundColor = [UIColor colorWithRed:0.230 green:0.692 blue:0.901 alpha:0.800];
    _menuButtonBackgroundView.layer.cornerRadius = 20;
    
    UIView *midView = [[UIView alloc] initWithFrame:CGRectMake(1, 1, 38, 38)];
    midView.layer.cornerRadius = 18;
    midView.layer.borderWidth = 1.5f;
    midView.layer.borderColor = [[UIColor colorWithRed:0.965 green:0.980 blue:0.992 alpha:1.000] CGColor];
    [_menuButtonBackgroundView addSubview:midView];
    [midView release];
    
    _menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _menuButton.frame = CGRectMake(6, 6, 28, 28);
    [_menuButton setImage:[UIImage imageNamed:@"iconfont-fenlei-1"] forState:UIControlStateNormal];
    [_menuButton addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
    [_menuButtonBackgroundView addSubview:_menuButton];
    
    self.fire = [[FireView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
    self.fire.center = CGPointMake(14, 35);
    self.fire.alpha = 0;
    [_menuButton addSubview:self.fire];
    [_fire release];
    self.fire.transform = CGAffineTransformRotate(self.fire.transform, M_PI);

    
    _backgroundMenuView = [[UIView alloc] init];
    _menuColor = [UIColor whiteColor];
    _buttonList = [[NSMutableArray alloc] initWithCapacity:titles.count];
    
    NSInteger index = 0;
    for (NSString *title in [titles copy])
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:title forState:UIControlStateNormal];
        button.layer.borderWidth = 2.5;
        button.layer.cornerRadius = 20;
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        button.layer.borderColor = [[UIColor whiteColor] CGColor];
        
        button.frame = CGRectMake(0, 0, 40, 40);
        button.center = CGPointMake(35, ([UIScreen mainScreen].bounds.size.height - 113) / 12 + ([UIScreen mainScreen].bounds.size.height - 113) / 6 * index);
        button.tag = index;
        [button addTarget:self action:@selector(onMenuButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_buttonList addObject:button];
        ++index;
    }

    
    return self;
}

- (void)insertMenuButtonOnView:(UIView*)view atPosition:(CGPoint)position
{
    _menuButtonBackgroundView.frame = CGRectMake(position.x, position.y, 40, 40);
    [view addSubview:_menuButtonBackgroundView];
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissMenu)];
    [_backgroundMenuView addGestureRecognizer:singleTap];
    
    for (UIButton *button in _buttonList)
    {
        [_backgroundMenuView addSubview:button];
    }

    _backgroundMenuView.frame = CGRectMake(view.frame.size.width, 0, 70, [UIScreen mainScreen].bounds.size.height - 113);
    _backgroundMenuView.backgroundColor = [UIColor colorWithRed:0.230 green:0.692 blue:0.901 alpha:0.800];
    [view addSubview:_backgroundMenuView];
}

#pragma mark - 
#pragma mark Menu button action

- (void)dismissMenuWithSelection:(UIButton*)button
{
    CGAffineTransform oldTransfrom = button.transform;
        [UIView animateWithDuration:0.3f
                              delay:0.0f
             usingSpringWithDamping:.2f
              initialSpringVelocity:10.f
                            options:0 animations:^{
                                button.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
                            }
                         completion:^(BOOL finished) {
                             button.transform = oldTransfrom;
                         }];
    
    [self.timer invalidate];
    if (button.tag != 5) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(dismissMenu) userInfo:nil repeats:NO];
    } else {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(dismissMenu) userInfo:nil repeats:NO];
    }
}

- (void)dismissMenu1 {
    if (_isOpen)
    {
        _isOpen = !_isOpen;
        [self performDismissAnimation];
    }
}


- (void)dismissMenu
{
    [_menuButton setImage:[UIImage imageNamed:@"iconfont-fenlei-1"] forState:UIControlStateNormal];
    if (_isOpen)
    {
        _isOpen = !_isOpen;
       [self performDismissAnimation];
    }
}

- (void)showMenu
{
    if (!_isOpen)
    {
        [_menuButton setImage:[UIImage imageNamed:@"iconfont-top(2)"] forState:UIControlStateNormal];
        _isOpen = !_isOpen;
        [self performSelectorInBackground:@selector(performOpenAnimation) withObject:nil];
        [self.timer invalidate];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(dismissMenu) userInfo:nil repeats:NO];
    } else {
        _isOpen = !_isOpen;
        [self.timer invalidate];
        CGRect oldRect = _menuButton.frame;
        [UIView animateWithDuration:0.8 animations:^{
            self.fire.alpha = 1;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:1 animations:^{
                [self.delegate top];
                [self performSelectorInBackground:@selector(performDismissAnimation) withObject:nil];
                _menuButton.frame = CGRectMake(oldRect.origin.x, oldRect.origin.y -200, oldRect.size.width, oldRect.size.height);
                _menuButton.alpha = 0;
                self.fire.alpha = 0;
            } completion:^(BOOL finished) {
                _menuButton.frame = oldRect;
                [UIView animateWithDuration:0.1 animations:^{
                    [_menuButton setImage:[UIImage imageNamed:@"iconfont-fenlei-1"] forState:UIControlStateNormal];
                    _menuButton.alpha = 1;
                }];
            }];
        }];
    }
}

- (void)onMenuButtonClick:(UIButton*)button
{
    if (button.tag != 5) {
        if ([self.delegate respondsToSelector:@selector(menuButtonClicked:)])
            [self.delegate menuButtonClicked:button.tag];
    }
    [self dismissMenuWithSelection:button];
}

#pragma mark -
#pragma mark - Animations

- (void)performDismissAnimation
{
    [UIView animateWithDuration:0.5 animations:^{
//        _menuButtonBackgroundView.alpha = 1.0f;
        _menuButtonBackgroundView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0);
        _backgroundMenuView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0);
    }];
    _isOpen = NO;
}

- (void)performOpenAnimation
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.3 animations:^{
//            _menuButtonBackgroundView.alpha = 0.0f;
            _menuButtonBackgroundView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, -70, 0);
            _backgroundMenuView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, -70, 0);
        }];
    });
    for (UIButton *button in _buttonList)
    {
        [NSThread sleepForTimeInterval:0.03f];
        dispatch_async(dispatch_get_main_queue(), ^{
            button.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 8, 0);
            [UIView animateWithDuration:0.3f
                                  delay:0.1f
                 usingSpringWithDamping:.3f
                  initialSpringVelocity:10.f
                                options:0 animations:^{
                                    button.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0);
                                }
                             completion:^(BOOL finished) {
                             }];
        });
    }
}

@end

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com
